package cn.lyjuan.first.netty.demo._1_nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NIOClient
{
    public static void main(String[] args) throws IOException
    {
        SocketChannel chl = SocketChannel.open();
        chl.connect(new InetSocketAddress("127.0.0.1", 8000));

        ByteBuffer buf = ByteBuffer.wrap("Hello".getBytes("UTF-8"));
        chl.write(buf);

        ByteBuffer inBuf = ByteBuffer.allocate(1024);
        int count = chl.read(inBuf);
        if (count > 0)
        {
            System.out.println("client read: " + new String(inBuf.array(), 0, count));
        }

        chl.write(ByteBuffer.wrap("bye".getBytes("UTF-8")));// 发送断开连接
    }
}
